package com.dd.service.impl;

import com.dd.entity.Employees;
import com.dd.enums.HttpEnum;
import com.dd.exception.BusinessException;
import com.dd.mapper.EmployeesMapper;
import com.dd.service.IEmployeesService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author dhd
 * @since 2024-10-21
 */
@Service
public class EmployeesServiceImpl implements IEmployeesService {

    @Autowired
    EmployeesMapper employeesMapper;

    @Override
    public int save(Employees employees) {
        return employeesMapper.insertBatch(Arrays.asList(employees));
    }

    @Override
    public int update(Employees employees) {
        return employeesMapper.updateSelective(employees);
    }

    @Override
    public int delete(Integer employeeId) {
        return employeesMapper.delByEmployeeId(employeeId);
    }

    @Override
    public Employees searchAllByLoginNameAndPassword(String loginName, String password) {
        //判断是否存在登录用户
        Employees employees = employeesMapper.searchAllByLoginName(loginName);
        if (employees == null){
            throw  new BusinessException(5001,"输入的账号不存在！");
        }
        if (!employees.getPassword().equals(password)){
            throw new BusinessException(5002,"密码错误！");
        }

        return employees;
    }

    @Override
    public PageInfo<Employees> getPage(String empName, String phoneNumber,Integer pageNum) {
        PageHelper.startPage(pageNum,5);
        List<Employees> employeeList = employeesMapper.selectAllByEmpNameAndPhoneNumber(empName, phoneNumber);
        PageInfo<Employees> pageInfo = new PageInfo<>(employeeList);
        return pageInfo;
    }

    @Override
    public Employees queryById(Integer id) {
        return employeesMapper.searchAllByEmployeeId(id);
    }
}
